package com.info.zhiduoduo.shiro;

import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.repository.entity.system.SystemManager;
import com.info.zhiduoduo.repository.mapper.system.ManagerMapper;

import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

public class AuthRealm extends AuthorizingRealm {

    @Resource
    ManagerMapper managerMapper;

    // @Resource
    // RoleUrlMapper roleUrlMapper;

    /**
     * 登录认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        String userName = usernamePasswordToken.getUsername();
        SystemManager systemManager = new SystemManager().selectOne(Condition.create().eq("loginname", userName));

        if(null == systemManager){
            throw new AccountException("用户名不正确");
        } else {
            if(1 != systemManager.getState()){
                throw new LockedAccountException();
            }
            if(StringUtils.isEmpty(systemManager.getPassword())){
                throw new AccountException("密码不正确");
            }
        }
        return new SimpleAuthenticationInfo(
                // 这里传入的是用户名，也可以直接传入user对象，直接传入用户名在授权部分就需要自己重新从数据库里取权限
                systemManager,
                // 密码
                systemManager.getPassword(),
                // salt = username + salt
                ByteSource.Util.bytes(userName + "salt"),
                // realm name
                getName());
    }

    /**
     * 权限认证
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //获取登录用户
        SystemManager systemManager = (SystemManager) principals.getPrimaryPrincipal();
        //根据当前用户查询数据库角色权限
        /*List<String> roleUrl = roleUrlMapper.findRoleUrl(systemManager.getRoleid());
        for(String url : roleUrl){
            authorizationInfo.addStringPermission(url);
        }*/
        authorizationInfo.addRole(String.valueOf(systemManager.getRoleid()));
        return authorizationInfo;
    }
}
